package Main;

import Entity.Tensor;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import util.StringUtil;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;


/**
 * Created by zxp on 2016/11/14.
 */
public class ProcessDBLP {


    static Set<String> stopWords = null;


    static Set<String> getStopWords() {
        if (stopWords == null) {
            stopWords = new HashSet<>();
            BufferedReader br = null;
            try {
                br = new BufferedReader(new InputStreamReader(new FileInputStream(Constraint.stopWordsFile), "UTF-8"));
                String line;
                while ((line = br.readLine()) != null) {
                    line = line.trim();
                    if (!line.equals("")) {
                        stopWords.add(line);
                    }
                }
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return stopWords;
    }


    /**
     * 将数据从文件中读入Tensor
     *
     * @return
     */
    public static Tensor fileToTensor() {
        BufferedReader br = null;

        int recordCount = 0;

        Set<String> stopSet = getStopWords();

        Tensor tensor = new Tensor();

        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(Constraint.dblpFile), "UTF-8"));
            String line;
            while ((line = br.readLine()) != null) {
                line = line.trim();
                if (!line.equals("")) {

                    String[] arr = line.split("\\|");

                    String time = arr[3]; // time
                    String conference = arr[2]; // conference

                    String termsStr = arr[1]; // title
                    if (arr.length > 4) { // 有的没有摘要
                        termsStr += " " + arr[4];
                    }

                    termsStr = StringUtil.removePunctuation(termsStr); // 处理标点符号
                    termsStr = StringUtil.removeSpareSpace(termsStr); // 处理多余的空格
                    termsStr = termsStr.toLowerCase(); // 全部转换成小写

//                    System.out.println(time+"    "+conference+"    "+termsStr);

                    String[] termsArr = termsStr.split(" ");
                    for (String curTerm : termsArr) {
                        if (curTerm.length() > 1 && !stopSet.contains(curTerm)) { // 当前term的长度必须大于1，且不是停用词
                            tensor.addTriple(time, conference, curTerm);
                        }
                    }
                }

                recordCount++;
                if (recordCount % 1000 == 0) {
                    System.out.println("已处理" + recordCount + "条记录");
                }

            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return tensor;
    }


    /**
     * 观察一个Tensor的细节
     *
     * @param tensor
     */
    public static void obverseTensor(Tensor tensor) {
        tensor.obverseTermsFre(10, 99999);
        tensor.obverseDocNumPerTerms(5, 99999);
        tensor.obverse_termsFre_and_docNumPerTerms(10, 99999, 5, 99999);



        tensor.generateTensorFile();

    }


    /**
     * 看一下每个会议有几条记录
     */
    static void testConference() {
        BufferedReader br = null;

        Map<String, Integer> conferenceMap = new HashMap<>();

        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(Constraint.dblpFile), "UTF-8"));
            String line;
            while ((line = br.readLine()) != null) {
                line = line.trim();
                if (!line.equals("")) {
                    String[] arr = line.split("\\|");
                    //System.out.println(arr.length);
                    //System.out.println(line);
                    String conference = arr[2];
                    if (!Constraint.conferenceSet.contains(conference) && !Constraint.noNeedConferenceSet.contains(conference)) {
                        System.out.println(line);
                    }
                    if (conferenceMap.get(conference) == null) {
                        conferenceMap.put(conference, 1);
                    } else {
                        conferenceMap.put(conference, conferenceMap.get(conference) + 1);
                    }
                }
            }
            br.close();

            for (Map.Entry<String, Integer> e : conferenceMap.entrySet()) {
                System.out.println(e.getKey() + "   " + e.getValue());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {

//        testConference();

        Tensor tensor = fileToTensor();
        obverseTensor(tensor);


    }


}
